package org.example.studentmanager.service.impl;

import org.example.studentmanager.dao.ClassDao;
import org.example.studentmanager.dao.CourseDao;
import org.example.studentmanager.dao.TeacherDao;
import org.example.studentmanager.entity.Clazz;
import org.example.studentmanager.entity.Course;
import org.example.studentmanager.entity.Teacher;
import org.example.studentmanager.service.CourseService;
import org.example.studentmanager.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseDao courseDao;
    @Autowired
    private ClassDao classDao;
    @Autowired
    private TeacherDao teacherDao;

    @Override
    public List<Course> queryCourses(String courseName) {
        List<Course> courses = courseDao.queryCourses(courseName);
        for (Course course : courses) {
            Integer teacherId = course.getTeacherId();
            if (teacherId != null) {
                Teacher teacher = teacherDao.queryTeacherByTeacherId(teacherId);
                if (teacher != null) {
                    course.setTeacherName(teacher.getTname());
                }
            }
        }
        return courses;
    }

    @Override
    public Integer insertCourse(Course course) {
        // 根据教师名查询教师信息
        Teacher teacher = teacherDao.queryTeacherInfoByTeacherName(course.getTeacherName());
        if (teacher == null) {
            return 0;
        }
        course.setTeacherId(teacher.getUserId());
        course.setStudentNumber(0)
                .setCreatedAt(LocalDateTime.now())
                .setUpdatedAt(LocalDateTime.now())
                .setIsDelete(false)
                .setStatus("yes")
                .setTeacherName(teacher.getTname());

        try {
            courseDao.insertCourse(course);
            classDao.insertClass(new Clazz(course.getCname(), course.getClasscode(), course.getCname(), "大一", course.getTeacherId()));
        } catch (Exception e) {
            return 0;
        }
        return 1;
    }

    @Override
    public Integer updateCourse(Course course) {
        return courseDao.updateCourse(course);
    }

    @Override
    public Course queryCourseByCourseId(Integer courseId) {
        Course course = courseDao.queryCourseByCourseId(courseId);
        if (course != null) {
            Teacher teacher = teacherDao.queryTeacherByTeacherId(course.getTeacherId());
            if (teacher != null) {
                course.setTeacherName(teacher.getTname());
            }
        }
        return course;
    }

    @Override
    @Transactional
    public boolean deleteCourse(Integer courseId) {
        // 先删除关联数据
        courseDao.deleteClassCourseByCourseId(courseId);
        courseDao.deleteCourseSelectionsStudentByCourseId(courseId);
        courseDao.deleteCourseSelectionsTeacherByCourseId(courseId);

        // 最后删除课程
        int courseDeleteResult = courseDao.deleteCourse(courseId);
        return courseDeleteResult > 0;
    }
}
